package cn.source.web.controller.system;

import cn.source.common.annotation.Log;
import cn.source.common.constant.UserConstants;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.enums.BusinessType;
import cn.source.common.utils.poi.ExcelUtil;
import cn.source.system.domain.SysCompany;
import cn.source.system.service.ISysCompanyService;
import cn.source.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业管理Controller
 *
 * @author sourcebyte.vip
 * @date 2023-10-19
 */
@RestController
@RequestMapping("/system/company")
public class SysCompanyController extends BaseController {
    @Autowired
    private ISysCompanyService sysCompanyService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询企业管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCompany sysCompany) {
        startPage();
        List<SysCompany> list = sysCompanyService.selectSysCompanyList(sysCompany);
        return getDataTable(list);
    }

    /**
     * 导出企业管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:company:export')")
    @Log(title = "企业管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCompany sysCompany) {
        List<SysCompany> list = sysCompanyService.selectSysCompanyList(sysCompany);
        ExcelUtil<SysCompany> util = new ExcelUtil<SysCompany>(SysCompany.class);
        util.exportExcel(response, list, "企业管理数据");
    }

    /**
     * 获取企业管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysCompanyService.selectSysCompanyById(id));
    }

    /**
     * 新增企业管理
     */
    @PreAuthorize("@ss.hasPermi('system:company:add')")
    @Log(title = "企业管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCompany sysCompany) {
        if (UserConstants.NOT_UNIQUE.equals(
                userService.checkUserNameUnique(sysCompany.getUserName()))) {
            return AjaxResult.error("新增用企业'" + sysCompany.getUserName() + "'失败，登录账号已存在");
        }
        return toAjax(sysCompanyService.insertSysCompany(sysCompany));
    }

    /**
     * 修改企业管理
     */
    @PreAuthorize("@ss.hasPermi('system:company:edit')")
    @Log(title = "企业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCompany sysCompany) {
        return toAjax(sysCompanyService.updateSysCompany(sysCompany));
    }

    /**
     * 删除企业管理
     */
    @PreAuthorize("@ss.hasPermi('system:company:remove')")
    @Log(title = "企业管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysCompanyService.deleteSysCompanyByIds(ids));
    }

    /**
     * 企业授权
     */
    @PreAuthorize("@ss.hasPermi('system:company:edit')")
    @Log(title = "企业管理", businessType = BusinessType.UPDATE)
    @PutMapping("/agent")
    public AjaxResult agent(@RequestBody SysCompany sysCompany) {
        return toAjax(sysCompanyService.agentSysCompany(sysCompany));
    }
}
